Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IR] Attribute system #51636

Merged

Conversation

zhangbo9674
Copy link
Contributor

@zhangbo9674 zhangbo9674 commented Mar 14, 2023

PR types

New features

PR changes

APIs

Describe

Pcard-67164

本 PR 为 Paddle 新 IR 的属性系统开发,属性系统与类型系统设计方案一致,类型系统开发 PR 见:(1)Type基础数据结构开发、(2)Type类型生成工具开发、(3)IrContext、(4)Dialect 及内置类型开发

1、PR 主要内容

1.1 Attribute 基础数据结构
  • attribute_base.h
    • class AbstractAttribute:将所有 Attribute 类共有的属性和行为抽象为一个 AbstractAttribute 类。Attribute 有 2 种:无参 Attribute、有参 Attribute,所有 Attribute 共有的属性就是 TypeId,因此构造一个类,将 TypeId 作为其成员。
    • class AttributeStorage:用于存储一个 Attribute 的所有信息,一个 Attribute 对象包含一个 AttributeStorage。无参类型 Attribute,信息包括:TypeId,因此 AttributeStorage 只需要包含 AbstractAttribute 即可;有参类型 Attribute,除了包含 AbstractAttribute/TypeId 之外,还需要包含参数信息,需要基于此派生出其他的AttributeStorage;
  • attribute.h
    • 定义 class Attribute,作为用户使用的唯一接口,是所有内置属性/自定义属性的基类;
1.2 Attribute 构造、注册、管理
  • attribute_base.h
    • 定义 AttributeManager 工具类,提供 get/RegisterAttribute 方法,用于向 IrContext 中获取、注册类型;
    • 定义 2个工具宏:DECLARE_ATTRIBUTE_UTILITY_FUNCTOR、REGISTER_ATTRIBUTE_2_IRCONTEXT,用于 Attribute 的定义及注册;
1.3 内置 Attribute

注:IrContext 及 Dialect 基础数据结构已在类型系统开发完成,本 PR 在 IrContext 及 Dialect 内添加 Attribute 相关接口及数据结构,详见:ir_context.hdialect.h

图片

2、Attribute 用法介绍

2.1 使用内置 Attribute:
// 获取 Attribute 对象
ir::IrContext *ctx = ir::IrContext::Instance();
ir::Attribute string_attr = ir::StringAttribute::get(ctx, "string_a", 8);
// 使用 isa 进行类型判断
std::cout << ir::StringAttribute::classof(string_attr) << std::endl; // 1
// 使用dyn_cast 进行类型转换
ir::StringAttribute string_attr_cast = string_attr.dyn_cast<ir::StringAttribute>();
std::cout << string_attr_cast.isa<ir::StringAttribute>() << std::endl; // 1

@paddle-bot
Copy link

paddle-bot bot commented Mar 14, 2023

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

Copy link
Contributor

@winter-wang winter-wang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

@zhiqiu zhiqiu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@zhangbo9674 zhangbo9674 merged commit 586d901 into PaddlePaddle:develop Mar 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants